package com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.unsafeevent.baqsjcommon.BaqsjCOMMON;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.bo.request.*;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.bo.response.*;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.model.BaqsjRecordMain;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.dao.BaqsjRecordMainDao;
import com.fufan.mtsaas.modules.unsafeevent.baqsjrecordmain.service.BaqsjRecordMainSvc;

import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 不良事件主记录 服务实现类
 * </p>
 *
 * @author Liby
 * @since 2025-02-18
 */
@Service("BaqsjRecordMainSvcImpl")
@Slf4j
@Primary
public class BaqsjRecordMainSvcImpl extends ServiceImpl<BaqsjRecordMainDao, BaqsjRecordMain> implements BaqsjRecordMainSvc {
    @Resource
    private BaqsjRecordMainDao targetDao;

    @Resource
    private BaqsjRecordEventProcess eventProcess;

    @Resource
    private BaqsjRecordEventQueryProcess eventQueryProcess;

    @Resource
    private ParamUtil paramUtil;


    @Override
    public Result doQueryPage(UnsafeeventQueryRequest map, RequestPojo requestPojo) {
        Result result = new Result();
        map.setCompanyId(Long.valueOf(requestPojo.getCustId()));

        Page<UnsafeeventRecordMainResponse> page = new Page<>();
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                targetDao.queryAllByLimit(map, page);
            }
        });

        // 分页
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);

        //分页查询
        List<UnsafeeventRecordMainResponse> list = targetDao.queryAllByLimit(map, page);

        page.setTotal((int) total);
        page.setRecords(list);

        result.setResData(page);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }

    @Override
    public Result<T> doQueryDetail(UnsafeeventQueryRequest map, RequestPojo requestPojo) {
        Result result = new Result();
        Page page = new Page<>();

        if (Objects.isNull(map.getId()) || map.getId() == 0) {
            result.setResStatus(ResStatus.STATUS_WRONGFUL);
            result.setResMsg(ResStatus.STATUS_WRONGFUL_MSG + "：id不能为空");
            return result;
        }

        result = eventQueryProcess.processEventData("", map, requestPojo);

        return result;
    }

    @Override
    public Result<T> doSummary(UnsafeeventQueryRequest map, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result<T> doSummaryReport(UnsafeeventQueryRequest map, RequestPojo requestPojo) {
        return null;
    }

    @Override
    @Transactional
    public Result<UnsafeeventAccidentChokingFoodResponse> doSaveAccidentChokingFood(UnsafeeventAccidentChokingFoodSaveRequest entity, RequestPojo requestPojo) {

        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_AccidentChokingFood, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventAccidentFallResponse> doSaveAccidentFall(UnsafeeventAccidentFallSaveRequest entity, RequestPojo requestPojo) {

        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_AccidentFall, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventAccidentHurtResponse> doSaveAccidentHurt(UnsafeeventAccidentHurtSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_AccidentHurt, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventAccidentOtherResponse> doSaveAccidentOther(UnsafeeventAccidentOtherSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_AccidentOther, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventAccidentOutResponse> doSaveAccidentOut(UnsafeeventAccidentOutSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_AccidentOut, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventAccidentSelfKillResponse> doSaveAccidentSelfKill(UnsafeeventAccidentSelfKillSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_AccidentSelfKill, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventDiagnosisDietWrongResponse> doSaveDiagnosisDietWrong(UnsafeeventDiagnosisDietWrongSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_DiagnosisDietWrong, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventDiagnosisInfusionReactionResponse> doSaveDiagnosisInfusionReaction(UnsafeeventDiagnosisInfusionReactionSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_DiagnosisInfusionReaction, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventDiagnosisMedicalHandleResponse> doSaveDiagnosisMedicalHandle(UnsafeeventDiagnosisMedicalHandleSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_DiagnosisMedicalHandle, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventDiagnosisOtherReactionResponse> doSaveDiagnosisOtherReaction(UnsafeeventDiagnosisOtherReactionSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_DiagnosisOtherReaction, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventDiagnosisSampleWrongResponse> doSaveDiagnosisSampleWrong(UnsafeeventDiagnosisSampleWrongSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_DiagnosisSampleWrong, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventDoctorAndPatientNegotiationResponse> doSaveDoctorAndPatientNegotiation(UnsafeeventDoctorAndPatientNegotiationSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_DoctorAndPatientNegotiation, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventHospitalInfectedResponse> doSaveHospitalInfected(UnsafeeventHospitalInfectedSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_HospitalInfected, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventMedicalBadReactionResponse> doSaveMedicalBadReaction(UnsafeeventMedicalBadReactionSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_MedicalBadReaction, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventMedicalWrongDrugResponse> doSaveMedicalWrongDrug(UnsafeeventMedicalWrongDrugSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_MedicalWrongDrug, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventMedicalInstrumentResponse> doSaveMedicalInstrument(UnsafeeventMedicalInstrumentSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_MedicalInstrument, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventOtherReactionGeneralResponse> doSaveOtherReactionGeneral(UnsafeeventOtherReactionGeneralSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_OtherReactionGeneral, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventOtherReactionPublicResponse> doSaveOtherReactionPublic(UnsafeeventOtherReactionPublicSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_OtherReactionPublic, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventOtherReactionPublicSecurityResponse> doSaveOtherReactionPublicSecurity(UnsafeeventOtherReactionPublicSecuritySaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_OtherReactionPublicSecurity, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventPreventionAndProtectionResponse> doSavePreventionAndProtection(UnsafeeventPreventionAndProtectionSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_PreventionAndProtection, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventSurgicalBadResponse> doSaveSurgicalBad(UnsafeeventSurgicalBadSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_SurgicalBad, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventSurgicalNonPlanedResponse> doSaveSurgicalNonPlaned(UnsafeeventSurgicalNonPlanedSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_SurgicalNonPlaned, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventSurgicalOperationBadResponse> doSaveSurgicalOperationBad(UnsafeeventSurgicalOperationBadSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_SurgicalOperationBad, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventTransfusionBloodReactionResponse> doSaveTransfusionBloodReaction(UnsafeeventTransfusionBloodReactionSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_TransfusionBloodReaction, entity, requestPojo);

        return result;
    }

    @Override
    public Result<UnsafeeventTransfusionWrongBloodResponse> doSaveTransfusionWrongBlood(UnsafeeventTransfusionWrongBloodSaveRequest entity, RequestPojo requestPojo) {
        Result result = eventProcess.processEventData(BaqsjCOMMON.EVENT_NO_TransfusionWrongBlood, entity, requestPojo);

        return result;
    }

    @Override
    public Result doApprove(UnsafeeventApproveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doDelete(UnsafeeventDeleteRequest map, RequestPojo requestPojo) {
        Result result = new Result();

        targetDao.deleteById(map.getId());

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }
}
